﻿using System;
using System.Collections.Generic;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using Secured3d.Auto.Pages;
using Secured3d.Auto.Utils;

namespace Secured3d.Auto.Helpers
{
    public class MyFilesBlock : BasePage
    {
        [FindsBy(How = How.CssSelector, Using = "#myTable2 tr[class ^= \"row\"]")] 
        private IList<IWebElement> files = null;

        [FindsBy(How = How.CssSelector, Using = "form[action=\"/myfiles\"]")] 
        private IWebElement myfilesForm = null;

        public int Count
        {
            get
            {
                if(IsAt)
                   return files.Count;
                throw new Exception("My Files Form is not available");
            }
        }

        private string LastAddedFileId
        {
            get
            {
                var lastAddedFile = myfilesForm.WaitUntilVisible().FindElement(By.CssSelector(".model"));
                return lastAddedFile.Text;
            }
        }


        private bool IsAt
        {
            get { return myfilesForm.WaitUntilVisible().Displayed; }
        }

        public ModelFile SelectFileById(string id)
        {
            var fileElement = Driver.FindElement(By.Id(id));
            return new ModelFile(fileElement);
        }

        public ModelFile SelectLastAddedFile()
        {
            var fileElement = Driver.FindElement(By.Id(LastAddedFileId));
            return new ModelFile(fileElement);
        }
    }

    public class ModelFile
    {
        private IWebElement fileBlock;

        public ModelFile(IWebElement fileBlock)
        {
            this.fileBlock = fileBlock;
        }

        public void Delete()
        {
            var deleteButton = fileBlock.FindElement(By.Id("delete"));
            deleteButton.Click();
        }

        public ChoosePrinterWindow Print()
        {
            var printButton = fileBlock.FindElement(By.CssSelector("input[value = \"Print\"]"));
            printButton.WaitUntilVisible().Click();
            return new ChoosePrinterWindow();
        }

        public int SuccessfullPrints
        {
            get
            {
                var successfulPrintsNumber =
                    fileBlock.FindElement(By.CssSelector("#myTable2 tr td a span[style *=\"green\"]")).Text;
                return Convert.ToInt32(successfulPrintsNumber);
            }
        }
    }

    public class ChoosePrinterWindow : BasePage
    {
        [FindsBy(How = How.CssSelector, Using = "#print_list #print_model")] 
        private IWebElement goPrintButton = null;

        [FindsBy(How = How.Id, Using = "confirm_settings")] 
        private IWebElement confirmPrintButton = null;

        [FindsBy(How = How.Id, Using = "secured3dContinueButton")] 
        private IWebElement continueInBackGroundButton = null;

        [FindsBy(How = How.CssSelector, Using = "#progressBar div")] 
        private IWebElement progressBar = null;

        [FindsBy(How = How.Id, Using = "secured3dContinueButton")] 
        private IWebElement finishButton;

        public ChoosePrinterWindow SelectPrinter()
        {
            goPrintButton.WaitUntilVisible().Click();
            return this;
        }

        public void ConfirmPrint()
        {
            confirmPrintButton.WaitUntilVisible().Click();

            while (progressBar.Text.Trim() != "100%")
            {
                Thread.Sleep(100);
            }

            finishButton.WaitUntilVisible().Click();
        }

       


    }
}
